package com.trussell.genesis;

import java.awt.Dimension;
import java.awt.event.KeyListener;

import com.jme.input.KeyInput;
import com.jme.system.DisplaySystem;
import com.jme.system.canvas.JMECanvasImplementor;
import com.jme.system.lwjgl.LWJGLSystemProvider;
import com.jmex.awt.input.AWTMouseInput;
import com.jmex.awt.lwjgl.LWJGLAWTCanvasConstructor;
import com.jmex.awt.lwjgl.LWJGLCanvas;

public class JMEPanel {

	private static JMEPanel instance;

	public static JMEPanel getInstance() {
		if (instance == null)
			instance = new JMEPanel();
		return instance;
	}

	private LWJGLCanvas canvas = null;
	private JMECanvasImplementor impl;

	private static final long serialVersionUID = 1L;

	public void init() {
		DisplaySystem display = DisplaySystem
				.getDisplaySystem(LWJGLSystemProvider.LWJGL_SYSTEM_IDENTIFIER);
		display.registerCanvasConstructor("AWT",
				LWJGLAWTCanvasConstructor.class);

		canvas = (LWJGLCanvas) display.createCanvas(768, 512);
		canvas.setPreferredSize(new Dimension(768, 512));
		canvas.setUpdateInput(true);
		canvas.setTargetRate(60);
		canvas.setBounds(0, 0, 512, 512);

		// Setup key and mouse input
		KeyInput.setProvider(KeyInput.INPUT_AWT);
		KeyListener kl = (KeyListener) KeyInput.get();
		canvas.addKeyListener(kl);
		AWTMouseInput.setup(canvas, false);

		// Important! Here is where we add the guts to the panel:
		impl = GenesisImplementor.getInstance();
		canvas.setImplementor(impl);
	}
	
	public LWJGLCanvas getCanvas() {
		return canvas;
	}
}
